using System;
using System.Globalization;
using System.Text.RegularExpressions;
using NUnit.Framework;

namespace Microsoft.WowAddonStudio.FrameXml.Serialization.Tests
{
	[TestFixture]
	public class ColorTest
	{
		private static readonly char listSeparator = CultureInfo.CurrentCulture.TextInfo.ListSeparator[0];
		private static readonly Regex colorStringRegex = new Regex(@"^(((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))" + listSeparator + @"){2}(((\d{1,2})|(1\d{2})|(2[0-4] \d)|(25[0-5]))){1}$", RegexOptions.Compiled);

		#region SetUp

		[SetUp]
		public void SetUp()
		{
		}

		#endregion

		#region TearDown

		[TearDown]
		public void TearDown()
		{
		}

		#endregion

		#region Tests

		[Test(Description = "")]
		public void Test_FromColor()
		{
			Color color = Color.FromColor(System.Drawing.Color.Blue);
			Console.WriteLine("A:{0},R:{1},G:{2},B:{3}", color.Alpha, color.Red, color.Green, color.Blue);
			Assert.AreEqual(1,color.Alpha);
			Assert.AreEqual(0, color.Red);
			Assert.AreEqual(1, color.Blue);
			Assert.AreEqual(0, color.Green);

			color = Color.FromColor(System.Drawing.Color.Red);
			Console.WriteLine("A:{0},R:{1},G:{2},B:{3}", color.Alpha, color.Red, color.Green, color.Blue);
			Assert.AreEqual(1, color.Alpha);
			Assert.AreEqual(1, color.Red);
			Assert.AreEqual(0, color.Blue);
			Assert.AreEqual(0, color.Green);

			color = Color.FromColor(System.Drawing.Color.Green);
			Console.WriteLine("A:{0},R:{1},G:{2},B:{3}", color.Alpha, color.Red, color.Green, color.Blue);
			Assert.AreEqual(1, color.Alpha);
			Assert.AreEqual(0, color.Red);
			Assert.AreEqual(0, color.Blue);
			Assert.AreEqual(0.5, color.Green);

			color = Color.FromColor(System.Drawing.Color.Yellow);
			Console.WriteLine("A:{0},R:{1},G:{2},B:{3}", color.Alpha, color.Red, color.Green, color.Blue);
			Assert.AreEqual(1, color.Alpha);
			Assert.AreEqual(1, color.Red);
			Assert.AreEqual(0, color.Blue);
			Assert.AreEqual(1, color.Green);

			color = Color.FromColor(System.Drawing.Color.FromArgb(0,128,0));
			Console.WriteLine("A:{0},R:{1},G:{2},B:{3}", color.Alpha, color.Red, color.Green, color.Blue);
			Assert.AreEqual(1, color.Alpha);
			Assert.AreEqual(0, color.Red);
			Assert.AreEqual(0, color.Blue);
			Assert.AreEqual(0.5, color.Green);


			color = new Color(){Alpha = 1, Green = 0.5f};
			System.Drawing.Color toColor = color.ToColor();
			Console.WriteLine("A:{0},R:{1},G:{2},B:{3}", toColor.A, toColor.R, toColor.G, toColor.B);
		}

		

		[Test(Description = "")]
		public void Test_RegExColor()
		{
			var textValue = "255" + listSeparator + "0" + listSeparator + "0";
			Assert.IsTrue(Match(textValue));

			textValue = "255" + listSeparator + "127" + listSeparator + "255";
			Assert.IsTrue(Match(textValue));

			textValue = "1" + listSeparator + "255" + listSeparator + "0" + listSeparator + "0";
			Assert.IsFalse(Match(textValue));

			textValue = "1" + listSeparator + "-255" + listSeparator + "0" + listSeparator + "0";
			Assert.IsFalse(Match(textValue));

			textValue = "0" + listSeparator + "0";
			Assert.IsFalse(Match(textValue));
		}


		/// <summary>
		/// Matches the specified text.
		/// </summary>
		/// <param name="text">The text.</param>
		/// <returns></returns>
		private static bool Match(string text)
		{
			Match match = colorStringRegex.Match(text);
			return match.Success;
		}

		#endregion
	}
}